package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.customview.AddProductInforPoupWindow;
import com.cloudsigns.factorymanager2.engineering.customview.DateViewDialog;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.Calendar;
import java.util.Locale;

public class ProduceEditProcessInformationActivity extends BaseActivity implements ItemClickListener {
    private String TAG = ProduceEditProcessInformationActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private EditText edt_process_num;
    private TextView edt_unit;
    private View view_unit;
    private ImageView img_unit;
    private EditText edt_start_date, edt_completed_date;
    private ImageView img_start_date, img_completed_date;
    private EditText edt_length, edt_hight, edt_diametra, edt_thickness;
    private ImageView cb_specific;
    private EditText edt_top, edt_bottom, edt_left, edt_right;
    private Button btn_save;

    private String unit = "套";
    private int diameterInt, heightInt, thickInt, lenghtInt, amountInt, topInt, bottomInt, leftInt, rightInt;
    private boolean isCheckBox = false;
    private AddProductInforPoupWindow addProductInforPoupWindow;
    private String datas[] = {"套", "只", "个", "箱", "件", "台", "条"};
    private WorkPartBean workPartBean;

    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_edit_information);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        edt_process_num = findViewById(R.id.edt_process_num);
        edt_unit = findViewById(R.id.edt_unit);
        view_unit = findViewById(R.id.view_unit);
        img_unit = findViewById(R.id.img_unit);
        edt_start_date = findViewById(R.id.edt_start_date);
        edt_completed_date = findViewById(R.id.edt_completed_date);
        img_start_date = findViewById(R.id.img_start_date);
        img_completed_date = findViewById(R.id.img_completed_date);
        edt_length = findViewById(R.id.edt_length);
        edt_hight = findViewById(R.id.edt_hight);
        edt_diametra = findViewById(R.id.edt_diametra);
        edt_thickness = findViewById(R.id.edt_thickness);
        cb_specific = findViewById(R.id.cb_specific);
        edt_top = findViewById(R.id.edt_top);
        edt_bottom = findViewById(R.id.edt_bottom);
        edt_left = findViewById(R.id.edt_left);
        edt_right = findViewById(R.id.edt_right);
        btn_save = findViewById(R.id.btn_save);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.edit_process_information));

        Intent intent = getIntent();
        if (null != intent) {
            workPartBean = (WorkPartBean) intent.getSerializableExtra(ConstantUtil.KEY_1);
        }

        CommonEnum.ProductShapeType shape = workPartBean.getShapeType().getType();
        if (null != shape) {
            if (shape == CommonEnum.ProductShapeType.ROUND) {
                edt_diametra.setText(workPartBean.getShapeType().getDiameterStr());
                edt_thickness.setText(workPartBean.getShapeType().getThickStr());
            } else if (shape == CommonEnum.ProductShapeType.RECTANGLE) {
                edt_length.setText(workPartBean.getShapeType().getWidthStr());
                edt_hight.setText(workPartBean.getShapeType().getHeightStr());
                edt_thickness.setText(workPartBean.getShapeType().getThickStr());
            } else if (shape == CommonEnum.ProductShapeType.SPECIALSHAPE) {
                cb_specific.setImageResource(R.mipmap.icon_radio_bg_yes);
                isCheckBox=true;
            }
        }

        edt_process_num.setText(workPartBean.getAmountStr());
        edt_unit.setText(workPartBean.getUnit());
        edt_start_date.setText(DateUtil.getYyyy_Mm_Dd(workPartBean.getStartDate()));
        edt_completed_date.setText(DateUtil.getYyyy_Mm_Dd(workPartBean.getCompleteDate()));
        edt_top.setText(workPartBean.getShapeType().getBorderTopStr());
        edt_bottom.setText(workPartBean.getShapeType().getBorderBottomStr());
        edt_left.setText(workPartBean.getShapeType().getBorderLeftStr());
        edt_right.setText(workPartBean.getShapeType().getBorderRightStr());
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        img_unit.setOnClickListener(this);
        img_start_date.setOnClickListener(this);
        img_completed_date.setOnClickListener(this);
        btn_save.setOnClickListener(this);
        cb_specific.setOnClickListener(this);
    }

    // 单位
    private void showUnitWindow(View anchor) {
        addProductInforPoupWindow = new AddProductInforPoupWindow(this, datas);
        addProductInforPoupWindow.setItemClick(this);
        addProductInforPoupWindow.showPopupWindow(anchor);
    }

    // 开工日期
    private void showStartDateDialog() {
        DateViewDialog endDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt_start_date.setText(time);
                    }
                });
        endDateChooseDialog.setTimePickerGone(true);
        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());
        endDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        endDateChooseDialog.showDateChooseDialog();
    }

    // 完工日期
    private void showCompletedDateDialog() {
        DateViewDialog endDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt_completed_date.setText(time);
                    }
                });
        endDateChooseDialog.setTimePickerGone(true);
        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());
        endDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        endDateChooseDialog.showDateChooseDialog();
    }

    // 保存
    private void save() {
        String quality = edt_process_num.getText().toString().trim();
        String startDate = edt_start_date.getText().toString().trim();
        String completedDate = edt_completed_date.getText().toString().trim();
        String topStr = edt_top.getText().toString().trim();
        String bottomStr = edt_bottom.getText().toString().trim();
        String leftStr = edt_left.getText().toString().trim();
        String rightStr = edt_right.getText().toString().trim();
        if (!TextUtils.isEmpty(quality)) {
            amountInt = Integer.parseInt(quality);
        }
        unit = edt_unit.getText().toString().trim();

        if (!TextUtils.isEmpty(topStr)) {
            topInt = Integer.parseInt(topStr);
        }
        if (!TextUtils.isEmpty(bottomStr)) {
            bottomInt = Integer.parseInt(bottomStr);
        }
        if (!TextUtils.isEmpty(leftStr)) {
            leftInt = Integer.parseInt(leftStr);
        }
        if (!TextUtils.isEmpty(rightStr)) {
            rightInt = Integer.parseInt(rightStr);
        }

        workPartBean.setAmount(amountInt);
        workPartBean.setUnit(unit);
        workPartBean.setStartDate(DateUtil.getMillis(startDate));
        workPartBean.setCompleteDate(DateUtil.getMillis(completedDate));
        workPartBean.getShapeType().setBorderTop(topInt);
        workPartBean.getShapeType().setBorderBottom(bottomInt);
        workPartBean.getShapeType().setBorderLeft(leftInt);
        workPartBean.getShapeType().setBorderRight(rightInt);

        if (TextUtils.isEmpty(edt_process_num.getText().toString())) {
            showToast(getResources().getString(R.string.input_process_num));
            return;
        } else if (TextUtils.isEmpty(edt_unit.getText().toString())) {
            showToast(getResources().getString(R.string.choose_unit));
            return;
        } else if (TextUtils.isEmpty(edt_start_date.getText().toString())) {
            showToast(getResources().getString(R.string.choose_start_date));
            return;
        } else if (TextUtils.isEmpty(edt_completed_date.getText().toString())) {
            showToast(getResources().getString(R.string.choose_complete_date));
            return;
        }

//        else if (TextUtils.isEmpty(edt_top.getText().toString())) {
//            Toast.makeText(this, "请输入上溜边尺寸", Toast.LENGTH_SHORT).show();
//            return;
//        } else if (TextUtils.isEmpty(edt_bottom.getText().toString())) {
//            Toast.makeText(this, "请输入下溜边尺寸", Toast.LENGTH_SHORT).show();
//            return;
//        } else if (TextUtils.isEmpty(edt_left.getText().toString())) {
//            Toast.makeText(this, "请输入左溜边尺寸", Toast.LENGTH_SHORT).show();
//            return;
//        } else if (TextUtils.isEmpty(edt_right.getText().toString())) {
//            Toast.makeText(this, "请输入右溜边尺寸", Toast.LENGTH_SHORT).show();
//            return;
//        }

        if (!isCheckBox
                && TextUtils.isEmpty(edt_length.getText().toString().trim())
                && TextUtils.isEmpty(edt_hight.getText().toString().trim())
                && TextUtils.isEmpty(edt_thickness.getText().toString().trim())
                && TextUtils.isEmpty(edt_diametra.getText().toString().trim())) {
            showToast(getResources().getString(R.string.input_size));
        } else {
            //矩形
            if (!TextUtils.isEmpty(edt_length.getText().toString().trim())
                    && !TextUtils.isEmpty(edt_hight.getText().toString().trim())
                    && !TextUtils.isEmpty(edt_thickness.getText().toString().trim())
                    && TextUtils.isEmpty(edt_diametra.getText().toString().trim())
                    && !isCheckBox) {

                lenghtInt = Integer.parseInt(edt_length.getText().toString().trim());
                heightInt = Integer.parseInt(edt_hight.getText().toString().trim());
                thickInt = Integer.parseInt(edt_thickness.getText().toString().trim());

                workPartBean.getShapeType().setWidth(lenghtInt);
                workPartBean.getShapeType().setHeight(heightInt);
                workPartBean.getShapeType().setThick(thickInt);
                workPartBean.getShapeType().setType(CommonEnum.ProductShapeType.RECTANGLE);
                workPartBean.setShapeMap(isCheckBox);

            } else if (!TextUtils.isEmpty(edt_diametra.getText().toString().trim())
                    && !TextUtils.isEmpty(edt_thickness.getText().toString().trim())
                    && TextUtils.isEmpty(edt_length.getText().toString().trim())
                    && TextUtils.isEmpty(edt_hight.getText().toString().trim())
                    && !isCheckBox) {// 圆形

                diameterInt = Integer.parseInt(edt_diametra.getText().toString().trim());
                thickInt = Integer.parseInt(edt_thickness.getText().toString().trim());

                workPartBean.getShapeType().setDiameter(diameterInt);
                workPartBean.getShapeType().setThick(thickInt);
                workPartBean.getShapeType().setType(CommonEnum.ProductShapeType.ROUND);
                workPartBean.setShapeMap(isCheckBox);

            } else if (isCheckBox) {// 异形
                workPartBean.getShapeType().setDesc("具体规格尺寸见图纸");
                workPartBean.getShapeType().setType(CommonEnum.ProductShapeType.SPECIALSHAPE);
                workPartBean.setShapeMap(isCheckBox);

                edt_diametra.setText("");
                edt_thickness.setText("");
                edt_length.setText("");
                edt_hight.setText("");
            }
            if (!isCheckBox) {
                workPartBean.getShapeType().setDesc("");
            }

            workPartBean.betweenDays();

            Intent mIntent = new Intent();
            mIntent.putExtra(ConstantUtil.WORKPARTBEAN, workPartBean);
            setResult(ConstantUtil.ADD_WORKPART_INFOMATION, mIntent);
            showToast(getResources().getString(R.string.save_success));
            finish();
        }
    }



    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 单位
            case R.id.img_unit:
                hideSoftInputView();
                showUnitWindow(view_unit);
                break;
            // 开工日期
            case R.id.img_start_date:
                showStartDateDialog();
                break;
            // 完工日期
            case R.id.img_completed_date:
                showCompletedDateDialog();
                break;
            // 具体规格尺寸见图纸
            case R.id.cb_specific:
                if (isCheckBox) {
                    cb_specific.setImageResource(R.mipmap.icon_radio_no);
                } else {
                    cb_specific.setImageResource(R.mipmap.icon_radio_bg_yes);
                    edt_diametra.setText("");
                    edt_thickness.setText("");
                    edt_length.setText("");
                    edt_hight.setText("");
                }
                isCheckBox = !isCheckBox;
                break;
            // 保存
            case R.id.btn_save:
                save();
                break;
        }
    }

    @Override
    public void itemClick(View view, int position) {
        edt_unit.setText(datas[position]);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isCheckBox = false;
    }
}
